<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>正则表达式 - MC&#39; blog</title><meta name="Description" content="MC&#39; Blog，不定期的技术分享"><meta property="og:title" content="正则表达式" />
<meta property="og:description" content="正则表达式 正则表达式，又称规则表达式。（英语：Regular Expression，在代码中常简写为regex、regexp或RE），计算机科" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" /><meta property="og:image" content="http://example.org/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2017-06-12T21:13:00+08:00" />
<meta property="article:modified_time" content="2017-06-12T21:13:00+08:00" /><meta property="og:site_name" content="MC&#39; Blog" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="http://example.org/logo.png"/>

<meta name="twitter:title" content="正则表达式"/>
<meta name="twitter:description" content="正则表达式 正则表达式，又称规则表达式。（英语：Regular Expression，在代码中常简写为regex、regexp或RE），计算机科"/>
<meta name="application-name" content="MC&#39; Blog">
<meta name="apple-mobile-web-app-title" content="MC&#39; Blog"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="http://example.org/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" /><link rel="prev" href="http://example.org/%E5%AD%A6%E7%94%9F%E6%88%90%E7%BB%A9%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" /><link rel="next" href="http://example.org/java%E5%9B%BD%E9%99%85%E5%8C%96/" /><link rel="stylesheet" href="/css/style.min.css"><link rel="preload" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css"></noscript><link rel="preload" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css"></noscript><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "正则表达式",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "http:\/\/example.org\/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F\/"
        },"image": ["http:\/\/example.org\/images\/Apple-Devices-Preview.png"],"genre": "posts","keywords": "算法, 字符串, 正则表达式","wordcount":  2732 ,
        "url": "http:\/\/example.org\/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F\/","datePublished": "2017-06-12T21:13:00+08:00","dateModified": "2017-06-12T21:13:00+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "http:\/\/example.org\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "MC"
            },"description": ""
    }
    </script></head>
    <body data-header-desktop="fixed" data-header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="MC&#39; blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw' aria-hidden='true'></i></span>MC&#39; Blog</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 所有文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/categories/documentation/"> 文档 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/woxin123" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i>  </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
                </a></div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="MC&#39; blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw' aria-hidden='true'></i></span>MC&#39; Blog</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">所有文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/categories/documentation/" title="">文档</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/woxin123" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
            </a></div>
    </div>
</header><div class="search-dropdown desktop">
        <div id="search-dropdown-desktop"></div>
    </div>
    <div class="search-dropdown mobile">
        <div id="search-dropdown-mobile"></div>
    </div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animate__animated animate__flipInX">正则表达式</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel="author" class="author"><i class="fas fa-user-circle fa-fw" aria-hidden="true"></i>MC</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/"><i class="far fa-folder fa-fw" aria-hidden="true"></i>数据结构与算法</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw" aria-hidden="true"></i>&nbsp;<time datetime="2017-06-12">2017-06-12</time>&nbsp;<i class="fas fa-pencil-alt fa-fw" aria-hidden="true"></i>&nbsp;约 2732 字&nbsp;
                <i class="far fa-clock fa-fw" aria-hidden="true"></i>&nbsp;预计阅读 6 分钟&nbsp;<span id="/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" class="leancloud_visitors" data-flag-title="正则表达式">
                        <i class="far fa-eye fa-fw" aria-hidden="true"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;次阅读
                    </span>&nbsp;</div>
        </div><div class="details toc" id="toc-static"  data-kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right" aria-hidden="true"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#正则表达式的规则">正则表达式的规则</a>
      <ul>
        <li><a href="#字符">字符</a></li>
        <li><a href="#字符类">字符类</a></li>
        <li><a href="#预定义字符类">预定义字符类</a></li>
        <li><a href="#posix-字符类仅-us-ascii">POSIX 字符类（仅 US-ASCII）</a></li>
        <li><a href="#javalangcharacter-类简单的-java-字符类型">java.lang.Character 类（简单的 java 字符类型）</a></li>
        <li><a href="#unicode-块和类别的类">Unicode 块和类别的类</a></li>
        <li><a href="#边界匹配器">边界匹配器</a></li>
        <li><a href="#greedy-数量词贪婪模式">Greedy 数量词（贪婪模式）</a></li>
        <li><a href="#possessive-数量词-占有模式">Possessive 数量词 （占有模式）</a></li>
        <li><a href="#logical-运算符">Logical 运算符</a></li>
      </ul>
    </li>
    <li><a href="#正则表达式的用法">正则表达式的用法</a>
      <ul>
        <li><a href="#正则表达式的用法匹配字符串">正则表达式的用法：匹配字符串</a></li>
        <li><a href="#正则表达式的用法替换分割">正则表达式的用法：替换、分割</a></li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h1 id="正则表达式">正则表达式</h1>
<p>正则表达式，又称规则表达式。（英语：Regular Expression，在代码中常简写为regex、regexp或RE），计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。（来源于百度百科）</p>
<h2 id="正则表达式的规则">正则表达式的规则</h2>
<h3 id="字符">字符</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">x</td>
<td style="text-align:left">字符 x</td>
</tr>
<tr>
<td style="text-align:left">\</td>
<td style="text-align:left">反斜线字符</td>
</tr>
<tr>
<td style="text-align:left">\0n</td>
<td style="text-align:left">带有八进制值0的字符 n (0 &lt;= n &lt;= 7)</td>
</tr>
<tr>
<td style="text-align:left">\0nn</td>
<td style="text-align:left">带有八进制值 0 的字符 nn (0 &lt;= n &lt;= 7)</td>
</tr>
<tr>
<td style="text-align:left">\0mnn</td>
<td style="text-align:left">带有八进制值0的字符mnn（0 &lt;= m &lt;= 3、0 &lt;= n &lt;= 7）</td>
</tr>
<tr>
<td style="text-align:left">\xhh</td>
<td style="text-align:left">带有十六进制值0x的字符hh</td>
</tr>
<tr>
<td style="text-align:left">\uhhhh</td>
<td style="text-align:left">带有十六进制值 0x 的字符hhhh</td>
</tr>
<tr>
<td style="text-align:left">\t</td>
<td style="text-align:left">制表符 (&rsquo;\u0009')</td>
</tr>
<tr>
<td style="text-align:left">\n</td>
<td style="text-align:left">新行（换行）符 (&rsquo;\u000A')</td>
</tr>
<tr>
<td style="text-align:left">\r</td>
<td style="text-align:left">回车符 (&rsquo;\u000D')</td>
</tr>
<tr>
<td style="text-align:left">\f</td>
<td style="text-align:left">换页符 (&rsquo;\u000C')</td>
</tr>
<tr>
<td style="text-align:left">\a</td>
<td style="text-align:left">报警 (bell) 符 (&rsquo;\u0007')</td>
</tr>
<tr>
<td style="text-align:left">\e</td>
<td style="text-align:left">转义符 (&rsquo;\u001B')</td>
</tr>
<tr>
<td style="text-align:left">\cx</td>
<td style="text-align:left">对应于 x 的控制符</td>
</tr>
</tbody>
</table>
<h3 id="字符类">字符类</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">[abc]</td>
<td style="text-align:left">a、b 或 c（简单类）</td>
</tr>
<tr>
<td style="text-align:left">[^abc]</td>
<td style="text-align:left">任何字符，除了 a、b 或 c（否定）</td>
</tr>
<tr>
<td style="text-align:left">[a-zA-Z]</td>
<td style="text-align:left">a 到 z 或 A 到 Z，两头的字母包括在内（范围）</td>
</tr>
<tr>
<td style="text-align:left">[a-d[m-p]]</td>
<td style="text-align:left">a 到 d 或 m 到 p：[a-dm-p]（并集）</td>
</tr>
<tr>
<td style="text-align:left">[a-z&amp;&amp;[def]]</td>
<td style="text-align:left">d、e 或 f（交集）</td>
</tr>
<tr>
<td style="text-align:left">[a-z&amp;&amp;[^bc]]</td>
<td style="text-align:left">a到z，除了b和c：[ad-z]（减去）</td>
</tr>
<tr>
<td style="text-align:left">[a-z&amp;&amp;[^m-p]]</td>
<td style="text-align:left">a 到 z，而非 m 到 p：[a-lq-z]（减去）</td>
</tr>
<tr>
<td style="text-align:left">[/u0041-/u0056]</td>
<td style="text-align:left">汉字</td>
</tr>
</tbody>
</table>
<h3 id="预定义字符类">预定义字符类</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">.</td>
<td style="text-align:left">任何字符（与行结束符可能匹配也可能不匹配）</td>
</tr>
<tr>
<td style="text-align:left">\d</td>
<td style="text-align:left">数字：[0-9]</td>
</tr>
<tr>
<td style="text-align:left">\D</td>
<td style="text-align:left">非数字： [^0-9]</td>
</tr>
<tr>
<td style="text-align:left">\s</td>
<td style="text-align:left">空白字符：[ \t\n\x0B\f\r]</td>
</tr>
<tr>
<td style="text-align:left">\S</td>
<td style="text-align:left">非空白字符：[^\s]</td>
</tr>
<tr>
<td style="text-align:left">\w</td>
<td style="text-align:left">单词字符：[a-zA-Z_0-9]</td>
</tr>
<tr>
<td style="text-align:left">\W</td>
<td style="text-align:left">非单词字符：[^\w]</td>
</tr>
</tbody>
</table>
<h3 id="posix-字符类仅-us-ascii">POSIX 字符类（仅 US-ASCII）</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">\p{Lower}</td>
<td style="text-align:left">小写字母字符：[a-z]</td>
</tr>
<tr>
<td style="text-align:left">\p{Upper}</td>
<td style="text-align:left">大写字母字符：[A-Z]</td>
</tr>
<tr>
<td style="text-align:left">\p{ASCII}</td>
<td style="text-align:left">所有 ASCII：[\x00-\x7F]</td>
</tr>
<tr>
<td style="text-align:left">\p{Alpha}</td>
<td style="text-align:left">字母字符：[\p{Lower}\p{Upper}]</td>
</tr>
<tr>
<td style="text-align:left">\p{Digit}</td>
<td style="text-align:left">十进制数字：[0-9]</td>
</tr>
<tr>
<td style="text-align:left">\p{Alnum}</td>
<td style="text-align:left">字母数字字符：[\p{Alpha}\p{Digit}]</td>
</tr>
<tr>
<td style="text-align:left">\p{Punct}</td>
<td style="text-align:left">标点符号：!&quot;#$%&amp;&rsquo;()*+,-./:;&lt;=&gt;?@[]^_`{</td>
</tr>
<tr>
<td style="text-align:left">\p{Graph}</td>
<td style="text-align:left">可见字符：[\p{Alnum}\p{Punct}]</td>
</tr>
<tr>
<td style="text-align:left">\p{Print}</td>
<td style="text-align:left">可打印字符：[\p{Graph}\x20]</td>
</tr>
<tr>
<td style="text-align:left">\p{Blank}</td>
<td style="text-align:left">空格或制表符：[ \t]</td>
</tr>
<tr>
<td style="text-align:left">\p{Cntrl}</td>
<td style="text-align:left">控制字符：[\x00-\x1F\x7F]</td>
</tr>
<tr>
<td style="text-align:left">\p{XDigit}</td>
<td style="text-align:left">十六进制数字：[0-9a-fA-F]</td>
</tr>
<tr>
<td style="text-align:left">\p{Space}</td>
<td style="text-align:left">空白字符：[ \t\n\x0B\f\r]</td>
</tr>
</tbody>
</table>
<h3 id="javalangcharacter-类简单的-java-字符类型">java.lang.Character 类（简单的 java 字符类型）</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">\p{javaLowerCase}</td>
<td style="text-align:left">等效于 java.lang.Character.isLowerCase()</td>
</tr>
<tr>
<td style="text-align:left">\p{javaUpperCase}</td>
<td style="text-align:left">等效于 java.lang.Character.isUpperCase()</td>
</tr>
<tr>
<td style="text-align:left">\p{javaWhitespace}</td>
<td style="text-align:left">等效于 java.lang.Character.isWhitespace()</td>
</tr>
<tr>
<td style="text-align:left">\p{javaMirrored}</td>
<td style="text-align:left">等效于 java.lang.Character.isMirrored()</td>
</tr>
</tbody>
</table>
<h3 id="unicode-块和类别的类">Unicode 块和类别的类</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">\p{InGreek}</td>
<td style="text-align:left">Greek 块（简单块）中的字符</td>
</tr>
<tr>
<td style="text-align:left">\p{Lu}</td>
<td style="text-align:left">大写字母（简单类别）</td>
</tr>
<tr>
<td style="text-align:left">\p{Sc}</td>
<td style="text-align:left">货币符号</td>
</tr>
<tr>
<td style="text-align:left">\P{InGreek}</td>
<td style="text-align:left">所有字符，Greek 块中的除外（否定）</td>
</tr>
<tr>
<td style="text-align:left">[\p{L}&amp;&amp;[^\p{Lu}]]</td>
<td style="text-align:left">所有字母，大写字母除外（减去）</td>
</tr>
</tbody>
</table>
<h3 id="边界匹配器">边界匹配器</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">^</td>
<td style="text-align:left">行的开头</td>
</tr>
<tr>
<td style="text-align:left">$</td>
<td style="text-align:left">行的结尾</td>
</tr>
<tr>
<td style="text-align:left">\b</td>
<td style="text-align:left">单词边界，就是非单词字符的地方</td>
</tr>
<tr>
<td style="text-align:left">\B</td>
<td style="text-align:left">非单词边界</td>
</tr>
<tr>
<td style="text-align:left">\A</td>
<td style="text-align:left">输入的开头</td>
</tr>
<tr>
<td style="text-align:left">\G</td>
<td style="text-align:left">上一个匹配的结尾</td>
</tr>
<tr>
<td style="text-align:left">\Z</td>
<td style="text-align:left">输入的结尾，仅用于最后的结束符（如果有的话）</td>
</tr>
<tr>
<td style="text-align:left">\z</td>
<td style="text-align:left">输入的结尾</td>
</tr>
</tbody>
</table>
<p>所谓的单词边界就是不能用\w全部匹配
举例：xiaoming\b
要匹配的字符xiaoming xiaoming222 xiaoming2()2
能匹配到的是第一个和第三个，因为第二个后面的全部都能用\w匹配。</p>
<h3 id="greedy-数量词贪婪模式">Greedy 数量词（贪婪模式）</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">X?</td>
<td style="text-align:left">X，一次或一次也没有</td>
</tr>
<tr>
<td style="text-align:left">X*</td>
<td style="text-align:left">X，零次或多次</td>
</tr>
<tr>
<td style="text-align:left">X+</td>
<td style="text-align:left">X，一次或多次</td>
</tr>
<tr>
<td style="text-align:left">X{n}</td>
<td style="text-align:left">X，恰好n次</td>
</tr>
<tr>
<td style="text-align:left">X{n,}</td>
<td style="text-align:left">X，至少n次</td>
</tr>
<tr>
<td style="text-align:left">X{n,m}</td>
<td style="text-align:left">X，至少n次，但是不超过m次</td>
</tr>
<tr>
<td style="text-align:left">Greedy模式：即贪婪模式，此模式匹配字符时一直会匹配下去，直到无法匹配。</td>
<td></td>
</tr>
</tbody>
</table>
<p>###Reluctant 数量词（勉强模式）</p>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">X??</td>
<td style="text-align:left">X，一次或一次也没有</td>
</tr>
<tr>
<td style="text-align:left">X*?</td>
<td style="text-align:left">X，零次或多次</td>
</tr>
<tr>
<td style="text-align:left">X+?</td>
<td style="text-align:left">X，一次或多次</td>
</tr>
<tr>
<td style="text-align:left">X{n}?</td>
<td style="text-align:left">X，恰好 n 次</td>
</tr>
<tr>
<td style="text-align:left">X{n,}?</td>
<td style="text-align:left">X，至少 n 次</td>
</tr>
<tr>
<td style="text-align:left">X{n,m}?</td>
<td style="text-align:left">X，至少 n 次，但是不超过 m 次</td>
</tr>
<tr>
<td style="text-align:left"><strong>每一个都比Greedy多一个?</strong></td>
<td></td>
</tr>
<tr>
<td style="text-align:left">勉强模式：匹配时是最小的字符。它与贪婪模式的对比如下。</td>
<td></td>
</tr>
</tbody>
</table>
<pre><code>public class GuiZeTest {
public static void main(String[] args) {
    String str = &quot;aaa, bbb&quot;;
    System.out.println(str.replaceFirst(&quot;\\w*&quot;, &quot;+&quot;)); // +, bbb
    System.out.println(str.replaceFirst(&quot;\\w*?&quot;, &quot;+&quot;)); // +aaa, bbb
    System.out.println(str.replaceFirst(&quot;\\w*+&quot;, &quot;+&quot;));
}
</code></pre>
<p>}</p>
<h3 id="possessive-数量词-占有模式">Possessive 数量词 （占有模式）</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">X?+</td>
<td style="text-align:left">X，一次或一次也没有</td>
</tr>
<tr>
<td style="text-align:left">X*+</td>
<td style="text-align:left">X，零次或多次</td>
</tr>
<tr>
<td style="text-align:left">X++</td>
<td style="text-align:left">X，一次或多次</td>
</tr>
<tr>
<td style="text-align:left">X{n}+</td>
<td style="text-align:left">X，恰好 n 次</td>
</tr>
<tr>
<td style="text-align:left">X{n,}+</td>
<td style="text-align:left">X，至少 n 次</td>
</tr>
<tr>
<td style="text-align:left">X{n,m}+</td>
<td style="text-align:left">X，至少 n 次，但是不超过 m 次</td>
</tr>
</tbody>
</table>
<p><strong>每一个都比Greedy多一个+</strong>
占有模式：只有Java才有的模式，一般较少使用。</p>
<h3 id="logical-运算符">Logical 运算符</h3>
<table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">XY</td>
<td style="text-align:left">X后跟Y</td>
</tr>
<tr>
<td style="text-align:left">X</td>
<td style="text-align:left">Y</td>
</tr>
</tbody>
</table>
<h2 id="正则表达式的用法">正则表达式的用法</h2>
<p>指定为字符串的正则表达式必须首先被编译为此类的实例。然后，可将得到的模式用于创建 Matcher 对象，依照正则表达式，该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中，所以多个匹配器可以共享同一模式。
典型的调用顺序是：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="n">Pattern</span> <span class="n">p</span> <span class="o">=</span> <span class="n">Pattern</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="s">&#34;a*b&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl"><span class="n">Matcher</span> <span class="n">m</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="na">matcher</span><span class="o">(</span><span class="s">&#34;aaaaab&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl"><span class="kt">boolean</span> <span class="n">b</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="na">matches</span><span class="o">();</span>
</span></span></code></pre></td></tr></table>
</div>
</div><blockquote>
<p><code>Pattern compile(String regex)</code>
将给定的正则表达式编译到模式中。
<code>public Matcher matcher(CharSequence input)</code>
创建一个匹配器，匹配给定的输入与此模式。
参数
input - 要匹配的字符序列
结果
这种模式的新匹配器
<code>public static boolean matches(String regex CharSequence input)</code>
编译给定的正则表达式，并尝试匹配给定的输入。
调用这种方便的方式的形式   Pattern.matches(regex, &gt;input);表现方式与表达式完全相同
<code>Pattern.compile(regex).matcher(input).matches()</code>
如果一个模式多次被使用，编译一次并重用它将比每次调用此方法更有效。
参数
regex - 要编译的表达式
input - 要匹配的字符序列
结果
正则表达式是否匹配输入</p>
</blockquote>
<p>由上文可知上面的三行代码可以写成：</p>
<p><code>boolean b = Pattern.matches(&quot;aaaaaab&quot;, &quot;a*b&quot;)</code></p>
<h3 id="正则表达式的用法匹配字符串">正则表达式的用法：匹配字符串</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="kn">import</span> <span class="nn">java.util.regex.*</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">class</span> <span class="nc">FindGroup</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">String</span> <span class="n">str</span> <span class="o">=</span> <span class="s">&#34;这儿有一些电话号码，我只需要13和15开头的电话号码。&#34;</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;13472011907，15189787288， 1789829828，1293141942&#34;</span><span class="o">;</span>        
</span></span><span class="line"><span class="cl">		<span class="n">Matcher</span> <span class="n">m</span> <span class="o">=</span> <span class="n">Pattern</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="s">&#34;((13\\d)|(15\\d))\\d{8}&#34;</span><span class="o">).</span><span class="na">matcher</span><span class="o">(</span><span class="n">str</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">while</span><span class="o">(</span><span class="n">m</span><span class="o">.</span><span class="na">find</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">m</span><span class="o">.</span><span class="na">group</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">	  <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><blockquote>
<p>find() : 返回目标中是否与Pattern中匹配的字符串
group() : 返回上一次与Pattern匹配的字符串
start() : 返回上一次与Pattern匹配字符串的开始位置（在原字符串中）
end() : 返回上一次与Pattern匹配字符串的结束位置加1
lookAt() : 返回目标字符串与Pattern是否匹配，此方法与matches()比较相似，但不同的是它不需要匹配整个字符串。
reset : 重新设置需要匹配的字符串
matches() : 返回目标是否与Pattern是否匹配</p>
</blockquote>
<p>下面是一些示例：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">java.util.regex.*</span><span class="o">;</span>
</span></span><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">StartAndEnd</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">String</span> <span class="n">str</span> <span class="o">=</span> <span class="s">&#34;Java is very easy&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;目标字符串是：&#34;</span> <span class="o">+</span> <span class="n">str</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">Pattern</span> <span class="n">p</span> <span class="o">=</span> <span class="n">Pattern</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="s">&#34;\\w+&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">Matcher</span> <span class="n">m</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="na">matcher</span><span class="o">(</span><span class="n">str</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">while</span><span class="o">(</span><span class="n">m</span><span class="o">.</span><span class="na">find</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">m</span><span class="o">.</span><span class="na">group</span><span class="o">()</span> <span class="o">+</span> <span class="s">&#34;起始位置：&#34;</span> <span class="o">+</span> <span class="n">m</span><span class="o">.</span><span class="na">start</span><span class="o">()</span> <span class="o">+</span> <span class="s">&#34;结束位置：&#34;</span> <span class="o">+</span> <span class="n">m</span><span class="o">.</span><span class="na">end</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">java.util.regex.*</span><span class="o">;</span>
</span></span><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MatchesTest</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">String</span> <span class="n">mails</span><span class="o">[]</span> <span class="o">=</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;1293141942@qq.com&#34;</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;mengchen@xiyou3g.com&#34;</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;ppap@abbc.org&#34;</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;ascsc@cacs.asa&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="o">};</span>
</span></span><span class="line"><span class="cl">        <span class="n">String</span> <span class="n">pattern</span> <span class="o">=</span> <span class="s">&#34;\\w{3,20}@\\w+\\.(com|net|cn|org|gov)&#34;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">Pattern</span> <span class="n">mp</span> <span class="o">=</span> <span class="n">Pattern</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="n">pattern</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">Matcher</span> <span class="n">mm</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">        <span class="k">for</span><span class="o">(</span><span class="n">String</span> <span class="n">mail</span> <span class="o">:</span> <span class="n">mails</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span><span class="o">(</span><span class="n">mm</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">mm</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="na">matcher</span><span class="o">(</span><span class="n">mail</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">            <span class="o">}</span>
</span></span><span class="line"><span class="cl">            <span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">mm</span><span class="o">.</span><span class="na">reset</span><span class="o">(</span><span class="n">mail</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">            <span class="o">}</span>
</span></span><span class="line"><span class="cl">            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">mail</span> <span class="o">+</span> <span class="o">(</span><span class="n">mm</span><span class="o">.</span><span class="na">matches</span><span class="o">()</span> <span class="o">?</span> <span class="s">&#34;是&#34;</span> <span class="o">:</span> <span class="s">&#34;不是&#34;</span><span class="o">)</span> <span class="o">+</span> <span class="s">&#34;一个邮箱地址。&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>事实上String中也提供了matches()方法</p>
<p><code>mials[0].matches(&quot;\\w{3,20}@\\w+\\.(com|net|cn|org|gov)&quot;)</code></p>
<h3 id="正则表达式的用法替换分割">正则表达式的用法：替换、分割</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">java.util.regex.*</span><span class="o">;</span>
</span></span><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ReplaceTest</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">String</span><span class="o">[]</span> <span class="n">str</span> <span class="o">=</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;Java is easy to study&#34;</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;I love Java&#34;</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;I don&#39;t konw to write something about Java&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="o">};</span>
</span></span><span class="line"><span class="cl">        <span class="n">Pattern</span> <span class="n">p</span> <span class="o">=</span> <span class="n">Pattern</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="s">&#34;Ja\\w+&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">Matcher</span> <span class="n">m</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">        <span class="k">for</span> <span class="o">(</span><span class="n">String</span> <span class="n">x</span> <span class="o">:</span> <span class="n">str</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="o">(</span><span class="n">m</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">m</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="na">matcher</span><span class="o">(</span><span class="n">x</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">            <span class="o">}</span>
</span></span><span class="line"><span class="cl">            <span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">m</span><span class="o">.</span><span class="na">reset</span><span class="o">(</span><span class="n">x</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">            <span class="o">}</span>
</span></span><span class="line"><span class="cl">            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">m</span><span class="o">.</span><span class="na">replaceAll</span><span class="o">(</span><span class="s">&#34;C语言&#34;</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>Matcher类提供了替换字符串的方法
replaceAll()：替换全部能匹配到的
replaceFirst() : 替换第一个匹配到的。
事实上，Java的String类也提供了这几个方法。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">java.util.Arrays</span><span class="o">;</span>
</span></span><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">StringReplaceTest</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">String</span><span class="o">[]</span> <span class="n">str</span> <span class="o">=</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;Java is easy to study&#34;</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;I love Java&#34;</span><span class="o">,</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;I don&#39;t konw to write something about Java&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="o">};</span>
</span></span><span class="line"><span class="cl">        <span class="k">for</span><span class="o">(</span><span class="n">String</span> <span class="n">x</span> <span class="o">:</span> <span class="n">str</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">replaceAll</span><span class="o">(</span><span class="s">&#34;java\\w+&#34;</span><span class="o">,</span> <span class="s">&#34;C语言&#34;</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Arrays</span><span class="o">.</span><span class="na">toString</span><span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">split</span><span class="o">(</span><span class="s">&#34; &#34;</span><span class="o">)));</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2017-06-12</span>
            </div></div>
        <div class="post-info-line">
            <div class="post-info-md"><span>
                            <a class="link-to-markdown" href="/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/index.md" target="_blank">阅读原始文档</a>
                        </span></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="http://example.org/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" data-title="正则表达式" data-hashtags="算法,字符串,正则表达式"><i class="fab fa-twitter fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="http://example.org/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" data-hashtag="算法"><i class="fab fa-facebook-square fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Hacker News" data-sharer="hackernews" data-url="http://example.org/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" data-title="正则表达式"><i class="fab fa-hacker-news fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="http://example.org/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" data-title="正则表达式"><i data-svg-src="https://cdn.jsdelivr.net/npm/simple-icons@7.3.0/icons/line.svg" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="http://example.org/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" data-title="正则表达式" data-ralateuid="xxxx"><i class="fab fa-weibo fa-fw" aria-hidden="true"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw" aria-hidden="true"></i>&nbsp;<a href="/tags/%E7%AE%97%E6%B3%95/">算法</a>,&nbsp;<a href="/tags/%E5%AD%97%E7%AC%A6%E4%B8%B2/">字符串</a>,&nbsp;<a href="/tags/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">正则表达式</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/%E5%AD%A6%E7%94%9F%E6%88%90%E7%BB%A9%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" class="prev" rel="prev" title="学生成绩管理系统"><i class="fas fa-angle-left fa-fw" aria-hidden="true"></i>学生成绩管理系统</a>
            <a href="/java%E5%9B%BD%E9%99%85%E5%8C%96/" class="next" rel="next" title="Java国际化">Java国际化<i class="fas fa-angle-right fa-fw" aria-hidden="true"></i></a></div>
</div>
<div id="comments"><div id="valine" class="comment"></div><noscript>
                Please enable JavaScript to view the comments powered by <a href="https://valine.js.org/">Valine</a>.
            </noscript></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.109.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.11"><i class="far fa-kiss-wink-heart fa-fw" aria-hidden="true"></i> LoveIt</a>
                </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw" aria-hidden="true"></i><span itemprop="copyrightYear">2023</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">MC</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw" aria-hidden="true"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw" aria-hidden="true"></i>
            </a>
        </div><link rel="stylesheet" href="/lib/valine/valine.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/valine@1.5.0/dist/Valine.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.38.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lunr@2.3.9/lunr.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.stemmer.support.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.zh.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.3.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.11/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.5.1/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":50},"comment":{"valine":{"appId":"QGzwQXOqs5JOhN4RGPOkR2mR-MdYXbMMI","appKey":"WBmoGyJtbqUswvfLh6L8iEBr","avatar":"mp","el":"#valine","emojiCDN":"https://cdn.jsdelivr.net/npm/emoji-datasource-google@14.0.0/img/google/64/","emojiMaps":{"100":"1f4af.png","alien":"1f47d.png","anger":"1f4a2.png","angry":"1f620.png","anguished":"1f627.png","astonished":"1f632.png","black_heart":"1f5a4.png","blue_heart":"1f499.png","blush":"1f60a.png","bomb":"1f4a3.png","boom":"1f4a5.png","broken_heart":"1f494.png","brown_heart":"1f90e.png","clown_face":"1f921.png","cold_face":"1f976.png","cold_sweat":"1f630.png","confounded":"1f616.png","confused":"1f615.png","cry":"1f622.png","crying_cat_face":"1f63f.png","cupid":"1f498.png","dash":"1f4a8.png","disappointed":"1f61e.png","disappointed_relieved":"1f625.png","dizzy":"1f4ab.png","dizzy_face":"1f635.png","drooling_face":"1f924.png","exploding_head":"1f92f.png","expressionless":"1f611.png","face_vomiting":"1f92e.png","face_with_cowboy_hat":"1f920.png","face_with_hand_over_mouth":"1f92d.png","face_with_head_bandage":"1f915.png","face_with_monocle":"1f9d0.png","face_with_raised_eyebrow":"1f928.png","face_with_rolling_eyes":"1f644.png","face_with_symbols_on_mouth":"1f92c.png","face_with_thermometer":"1f912.png","fearful":"1f628.png","flushed":"1f633.png","frowning":"1f626.png","ghost":"1f47b.png","gift_heart":"1f49d.png","green_heart":"1f49a.png","grimacing":"1f62c.png","grin":"1f601.png","grinning":"1f600.png","hankey":"1f4a9.png","hear_no_evil":"1f649.png","heart":"2764-fe0f.png","heart_decoration":"1f49f.png","heart_eyes":"1f60d.png","heart_eyes_cat":"1f63b.png","heartbeat":"1f493.png","heartpulse":"1f497.png","heavy_heart_exclamation_mark_ornament":"2763-fe0f.png","hole":"1f573-fe0f.png","hot_face":"1f975.png","hugging_face":"1f917.png","hushed":"1f62f.png","imp":"1f47f.png","innocent":"1f607.png","japanese_goblin":"1f47a.png","japanese_ogre":"1f479.png","joy":"1f602.png","joy_cat":"1f639.png","kiss":"1f48b.png","kissing":"1f617.png","kissing_cat":"1f63d.png","kissing_closed_eyes":"1f61a.png","kissing_heart":"1f618.png","kissing_smiling_eyes":"1f619.png","laughing":"1f606.png","left_speech_bubble":"1f5e8-fe0f.png","love_letter":"1f48c.png","lying_face":"1f925.png","mask":"1f637.png","money_mouth_face":"1f911.png","nauseated_face":"1f922.png","nerd_face":"1f913.png","neutral_face":"1f610.png","no_mouth":"1f636.png","open_mouth":"1f62e.png","orange_heart":"1f9e1.png","partying_face":"1f973.png","pensive":"1f614.png","persevere":"1f623.png","pleading_face":"1f97a.png","pouting_cat":"1f63e.png","purple_heart":"1f49c.png","rage":"1f621.png","relaxed":"263a-fe0f.png","relieved":"1f60c.png","revolving_hearts":"1f49e.png","right_anger_bubble":"1f5ef-fe0f.png","robot_face":"1f916.png","rolling_on_the_floor_laughing":"1f923.png","scream":"1f631.png","scream_cat":"1f640.png","see_no_evil":"1f648.png","shushing_face":"1f92b.png","skull":"1f480.png","skull_and_crossbones":"2620-fe0f.png","sleeping":"1f634.png","sleepy":"1f62a.png","slightly_frowning_face":"1f641.png","slightly_smiling_face":"1f642.png","smile":"1f604.png","smile_cat":"1f638.png","smiley":"1f603.png","smiley_cat":"1f63a.png","smiling_face_with_3_hearts":"1f970.png","smiling_imp":"1f608.png","smirk":"1f60f.png","smirk_cat":"1f63c.png","sneezing_face":"1f927.png","sob":"1f62d.png","space_invader":"1f47e.png","sparkling_heart":"1f496.png","speak_no_evil":"1f64a.png","speech_balloon":"1f4ac.png","star-struck":"1f929.png","stuck_out_tongue":"1f61b.png","stuck_out_tongue_closed_eyes":"1f61d.png","stuck_out_tongue_winking_eye":"1f61c.png","sunglasses":"1f60e.png","sweat":"1f613.png","sweat_drops":"1f4a6.png","sweat_smile":"1f605.png","thinking_face":"1f914.png","thought_balloon":"1f4ad.png","tired_face":"1f62b.png","triumph":"1f624.png","two_hearts":"1f495.png","unamused":"1f612.png","upside_down_face":"1f643.png","weary":"1f629.png","white_frowning_face":"2639-fe0f.png","white_heart":"1f90d.png","wink":"1f609.png","woozy_face":"1f974.png","worried":"1f61f.png","yawning_face":"1f971.png","yellow_heart":"1f49b.png","yum":"1f60b.png","zany_face":"1f92a.png","zipper_mouth_face":"1f910.png","zzz":"1f4a4.png"},"enableQQ":false,"highlight":true,"lang":"zh-CN","pageSize":10,"placeholder":"你的评论 ...","recordIP":true,"serverURLs":"https://leancloud.hugoloveit.com","visitor":true}},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
